using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MoEngine
{
    public class ModelNode : SceneNode
    {
        Model m_Model;

        public ModelNode() : base() { }

        public ModelNode(string modelName)
            : this()
        {
            LoadModel(modelName);
        }

        public void LoadModel(string modelName)
        {
            SetModel(Core.Content.Load<Model>(modelName));
        }

        public void SetModel(Model model)
        {
            m_Model = model;
            //CreateBoundingSphere();
        }

        public override void Render()
        {
            Matrix[] transforms = new Matrix[m_Model.Bones.Count];
            m_Model.CopyAbsoluteBoneTransformsTo(transforms);

            //Draw the model, a model can have multiple meshes, so loop
            foreach (ModelMesh mesh in m_Model.Meshes)
            {
                //This is where the mesh orientation is set, as well as our camera and projection
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * Transform;
                    effect.View = m_Scene.Camera.ViewMatrix;
                    effect.Projection = m_Scene.Camera.ProjMatrix;
                }
                //Draw the mesh, will use the effects set above.
                mesh.Draw();
            }

            base.Render();
        }
    }
}
